﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Web.Wap.Code;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using ZLCMS.Data.Model;

namespace ZLCMS.Web.Wap.Member
{
    public partial class ChangeOwner : MemberPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //由于获取goto刚好与登录成功的goto参数冲突，默认固定跳转到商城首页
            //string url = Request["goto"];
            //if (string.IsNullOrEmpty(url))
            //{
            //    url = "/wap/shop/";
            //}
            string url = "/wap/shop/";

            if (!string.IsNullOrEmpty(CurrentMember.OwnerAccount) && CurrentMember.OwnerAccount != "admin")
            {
                Response.Redirect(url);
            }
            string owner = Request["owner"];
            using(ISession session = SessionFactoryHelper.CreateSession())
            {
                SysUserInfo user = session.CreateQuery("from SysUserInfo where UserAccount=:acc")
                    .SetParameter("acc", owner)
                    .UniqueResult<SysUserInfo>();

                if (user != null && user.Status == "1")
                {
                    session.CreateSQLQuery("update member_info set owner_account=:owner,dept_code=:dept where account_id=:acc")
                        .SetParameter("owner", owner)
                        .SetParameter("dept", user.DeptCode)
                        .SetParameter("acc", CurrentMemberAccount)
                        .ExecuteUpdate();

                    Response.Redirect(url);
                }
                else
                {
                    Response.Write("参数错误");

                    Response.End();
                }
            }
        }
    }
}